{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CRC Polynomial 0x9EB2\n",
    "\n",
    "\\begin{equation*}\n",
    "P(x) = x^{16}+x^{15}+x^{12}+x^{11}+x^{10}+x^{9}+x^{7}+x^{5}+x^{4}+x^{1}\n",
    "\\end{equation*}\n",
    "```\n",
    "bits in polynomial\n",
    "\n",
    "16 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1  0\n",
    "1  1  0  0  1  1  1  1  0  1  0  1  1  0  0  1  0\n",
    "--> 19EB2\n",
    "```\n",
    "\n",
    "\n",
    "# Requirements\n",
    "\n",
    "* install crcmod\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import crcmod"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## construct crc-function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "poly = 0x19eb2\n",
    "initCrc = 0\n",
    "rev = True\n",
    "xorOut = 0\n",
    "crc16fun = crcmod.mkCrcFun(poly=poly, initCrc=initCrc, rev=rev, xorOut=xorOut)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example message\n",
    "\n",
    "Sequence missing: CRC output of 0x02 0x12 0xB1 0xB2 should be 0xC4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x02\\x12\\xb1\\xb2'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'0x5c1a'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = b'\\x02\\x12\\xB1\\xB2'\n",
    "print(data)\n",
    "check = crc16fun(data)\n",
    "hex(check)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code Generation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "crc16 = crcmod.Crc(poly=poly, initCrc=initCrc, rev=rev, xorOut=xorOut)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = \"crc16function.c\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(filename, 'w') as fp:\n",
    "    crc16.generateCode(functionName=\"crc16\", out=fp, dataType='uint8_t', crcType='uint8_t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
